<?php
namespace app\controller;

use app\BaseController;
use app\controller\model\Tables;
use liliuwei\think\Auth;
use think\facade\Session;
use think\facade\Db;
class Supplier extends BaseController
{
	protected $getpath="";
	protected $gobaluser="";
	protected $tables;
	public function __construct(){
		checkuser();
		$this->gobaluser=Session::get("uid");
		$this->tables=new Tables();
	}
	public function getSupplierAmount(){
		$id = request()->param("id");
		$supplier = DB::name($this->tables->supplier)->where("id",$id)->find();
		if(!$supplier)echojson([],1,'ID错误');
		$model = new model\Supplier($supplier['id']);
		$amount = $model->account();
		if($amount===false)echojson([],1,'更新失败');
		DB::name($this->tables->supplier)->where("id",$id)->update(['amount'=>$amount]);
		echojson([],0,'余额：'.$amount);
	}
	public function list(){
		$classesModel = new \app\controller\model\Classes();
		return view("index")->assign(['cids'=>$classesModel->rClassessToOption(265)]);
	}
	public function listAdd(){
		$id = request()->param("id");
		$id<1?$id=0:'';
		if($id>0){
			$re = DB::name($this->tables->supplier)->where("id",$id)->find();
		}else{
			$re = ['cids'=>0];
		}
		$classesModel = new \app\controller\model\Classes();
		return view("add")->assign(['data'=>$re,'cids'=>$classesModel->rClassessToOption(265,$re['cids'])]);
	}
	public function listGet(){
		$cid = request()->param("cid");
		$cid>0?$where="cid=$cid":$where='1=1';
		$status = request()->param("status");
		$status==-1?$where.=" and status=-1":$where.=' and status=1';
		$cid = request()->param("cid");
		$cid>1?$where.=" and cid=$cid":'';
		$title = request()->param("title");
		$title!=''?$where.=" and title like '%$title%'":'';
		$res = getpagedata($this->tables->supplier,"",true,$where);
		$count = $res[1];
		$res = $res[0];
		$cids = r_classes(265);
		$res = $res->all();
		foreach ($res as $k=>$v){
			$res[$k]['cids']=rclasstitle($cids,$v['cids']);
		}
		pagejson($res,0,'',$count);
	}
	public function listSave(){
		$data = request()->param();
		unset($data['id']);
		$id = request()->param("id");
		foreach ($data as $v){
			if($v=='')echojson([],1,'所有必填');
		}
		$data['addTime']=nowTime();
		if($id>0){
			$re = r_one($this->tables->supplier,"id=$id");
			if($re){
				DB::name($this->tables->supplier)->where("id",$id)->update($data);
				echojson([],0,'更新成功');
			}else{
				echojson([],1,'ID错误');
			}
		}else{
			$re = DB::name($this->tables->supplier)->insert($data);
			if($re){
				echojson([],0,'添加成功');
			}else{
				echojson([],1,'ID错误');
			}
		}
	}


}
